/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rcnss.data;
import java.util.*;
import java.sql.*;
/**
 *
 * @author Guesmi
 */
public class Categorie extends ITable {

    private static final String Table ="CATEGORIE";

    private int CAT_ID ;
    private String CAT_LIB ;

    /*Functions*/
    public void Categorie(){}

    public int GetCodeGategorie(){return this.CAT_ID;}
    public String GetLabelCategorie(){return this.CAT_LIB;}

    public void SetCodeGategorie(int cod){this.CAT_ID = cod ;}
    public void SetLabelCategorie(String lab){this.CAT_LIB = lab ;}
    
    public void LoadFromDB (String MAT){
        //ConnexionBD db = new ConnexionBD();
        try{
        ResultSet res = db.executerSelect(" Select * from "+ Categorie.Table +" where CATEG_CODE ='" + MAT + "'");
        if(res.next())
             {
                this.CAT_ID = res.getInt("CAT_ID");
                this.CAT_LIB = res.getString("CAT_LIB");
             
                System.out.println(this.CAT_ID);
                System.out.println(this.CAT_LIB);
             }
        else
            System.out.println("pas de categorie ou faute de requete !!");
        }catch(Exception sqle){}
    }

    public void  SaveToDB() throws Exception
    {
            db.InsertNewLine(Categorie.Table,"CAT_ID",Integer.toString(this.CAT_ID),"CAT_LIB","'"+this.CAT_LIB+"'");        
    }

    /*************************************************************************
    *                             STATIC FUCTIONS
    **************************************************************************/


    public static Categorie SearchByID(int COD){

        Categorie cat = new Categorie();
        try{
            ResultSet res = db.executerSelect("select * from "+ Categorie.Table +" where CAT_ID ='"+ COD +"'");
            while (res.next())
            {
                cat.CAT_ID = res.getInt("CAT_ID");
                cat.CAT_LIB = res.getString("CAT_LIB");
            }

       }catch(Exception sqle){}

       return cat ;
    }



    public static ArrayList<Categorie> SearchByNom (String nom)
    {
        ArrayList<Categorie> list = new ArrayList<Categorie>();
        try{
            ResultSet res = db.executerSelect("select * from "+ Categorie.Table +" where CATEG_LIB ='"+ nom +"'");
            while (res.next())
            {
                Categorie cat = new Categorie ();
                cat.CAT_ID = res.getInt("CAT_ID");
                cat.CAT_LIB = res.getString("CAT_LIB");
                list.add(cat);
            }
            System.out.println("La liste contient :"+ list.size() +" Categorie .");

        }catch(Exception sql){}
        return list ;
    }

    public static ArrayList<Categorie> getAll ()
    {
        ArrayList<Categorie> list = new ArrayList<Categorie>();
        try{
            ResultSet res = db.executerSelect("select * from "+ Table);
            while (res.next())
            {
                Categorie cat = new Categorie ();
                cat.CAT_ID = res.getInt("CAT_ID");
                cat.CAT_LIB = res.getString("CAT_LIB");
                list.add(cat);
            }
            System.out.println("La liste contient :"+ list.size() +" Categorie .");

        }catch(Exception sql){}
        return list ;
    }

    @Override
    public String toString()
    {
        return this.CAT_LIB;
    }
}